﻿using Microsoft.Office.Interop.Excel;
using Microsoft.Win32;
using MiniExcelLibs;
using OfficeOpenXml.FormulaParsing.Excel.Functions.RefAndLookup;
using PanhuaWin.工厂;
using PanhuaWin.系统管理;
using PanhuaWin.通用;
using Sunny.UI;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Printing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Controls.Primitives;
using System.Windows.Forms;
using DataTable = System.Data.DataTable;

namespace PanhuaWin.车间.轧硬
{
    public partial class Form轧硬报工维护 : UIPage

    {


        string 工厂guid1;
        string 部门编号1;
        string FormGuid;
        int 开始行;
        int 结束行;
        string 部门guid;
        string tempguid;
        string 轧硬原卷guid;
        string FormWorkType = "酸连轧";
        decimal 卷的总重量;
        public Form轧硬报工维护()
        {
            InitializeComponent();
        }

        private void uiButton选择卷_Click(object sender, EventArgs e)
        {

        }

        private void uiLabel2_Click(object sender, EventArgs e)
        {

        }

        private void Form轧硬报工维护_Resize(object sender, EventArgs e)
        {


        }

        private void Form轧硬报工维护_Load(object sender, EventArgs e)
        {
            tempguid = Guid.NewGuid().ToString();
            this.uiDatePicker报工日期.Value = pub.刷新时间();
            this.uiDatePicker1.Value =  DateTime.Today;
            this.uiDatePicker2.Value = DateTime.Today;
            this.uiTextBox工厂名称.Text = pub.全局工厂名称;

            pub.ListWareHouse(this.uiComboBox仓库, pub.全局工厂guid);
            pub.ListMaterial(pub.全局工厂编号, FormWorkType, this.uiComboBox新材质);
            pub.ListWorkLine(pub.全局工厂编号, FormWorkType, this.uiComboBox产线);
            pub.ListWorkType(pub.全局工厂编号, FormWorkType, this.uiComboBox去向);
            pub.ListWorkTypeAll(pub.全局工厂编号, FormWorkType, this.uiComboBox查询去向);
            pub.ListRank(pub.全局工厂编号, this.uiComboBox质量等级);
            pub.ListRankAll(pub.全局工厂编号, this.uiComboBox查询等级);
            pub.ListDayShift(pub.全局工厂编号, this.uiComboBoxDayShift);
            pub.List品名(this.uiComboBox品名, "轧硬", this.uiComboBox质量等级.SelectedText);
            pub.ListQualityStandard(FormWorkType, this.uiComboBox执行标准);
            pub.ListWorkLineAll(pub.全局工厂编号, FormWorkType, this.uiComboBox查询产线);
            pub.ListWorkTeamAll(pub.全局工厂编号, this.uiComboBox产线.SelectedValue.ToString(), this.uiComboBox查询班组);
            pub.ListQualityInfo(pub.全局工厂编号, FormWorkType, uiComboBox质量信息);
            string 当前班组 = pub.Get员工所属班组(pub.sys_user.UserTrueName);
            if (当前班组 == "")
            {
                pub.ListWorkTeam(pub.全局工厂编号, this.uiComboBox产线.SelectedValue.ToString(), this.uiComboBoxWorkTeam);
            }
            else
            {
                this.uiComboBoxWorkTeam.Items.Add(当前班组);
                this.uiComboBoxWorkTeam.SelectedIndex = 0;
            }
            清理自己mac的Temp_轧硬报工原卷占用();
            uiComboBox包装.Items.Add("精包");
            uiComboBox包装.Items.Add("简包");
            uiComboBox包装.Items.Add("普包");
            uiComboBox包装.Items.Add("裸包");
            uiComboBox包装.Items.Add("无");
        }

        private void 清理自己mac的Temp_轧硬报工原卷占用()
        {
            string sql;
            sql = "delete Temp_轧硬报工原卷占用 where mac='" + pub.getMac() + "' ";
            pub.ExecuteSql(pub.ConnectionString, sql, "清理自己mac的Temp_轧硬报工原卷占用,Temp_轧硬报工原卷占用");
        }

        private void uiLabel31_Click(object sender, EventArgs e)
        {

        }

        private void uiLabel33_Click(object sender, EventArgs e)
        {

        }

        private void uiButton选择已发未完工_Click(object sender, EventArgs e)
        {
            //CleallAll();

            //Form已酸洗未轧硬 form已酸洗未轧硬 = new Form已酸洗未轧硬();
            //form已酸洗未轧硬.工厂编号 = pub.全局工厂编号;
            //删除自己的tempguid的数据(tempguid);  //Temp_轧硬报工原卷占用
            //if (form已酸洗未轧硬.ShowDialog(this) == DialogResult.OK)
            //{
            //    this.uiTextBox原料卷号.Text = form已酸洗未轧硬.卷信息.OutputVolumeNew;
            //    轧硬原卷guid = pub.Get轧硬报工guid(this.uiTextBox原料卷号.Text);
            //    if (轧硬原卷guid == "")
            //    {
            //        MessageBox.Show("投入卷号选择有问题，请联系一下系统管理员处理: " + this.uiTextBox原料卷号.Text, "提示", MessageBoxButtons.OK);
            //        return;
            //    }
            //    this.uiTextBox产出卷号.Text = pub.去掉前缀(this.uiTextBox原料卷号.Text);//不含前缀，不含后面的串号
            //                                                                //this.uiTextBox存货编号1.Text = form已酸洗未轧硬.卷信息.InventoryCode;
            //                                                                //this.uiTextBox存货名称1.Text = form已酸洗未轧硬.卷信息.InventoryName;


            //    uiTextBox规格型号1.Text = form已酸洗未轧硬.卷信息.OutputSize;
            //    string 厚度 = "";
            //    string 宽度 = "";
            //    string 虚拟值 = "";
            //    pub.FromSize(uiTextBox规格型号1.Text, out 厚度, out 宽度, out 虚拟值);
            //    this.uiTextBox厚度1.Text = 厚度;
            //    this.uiTextBox宽度1.Text = 宽度;
            //    this.uiTextBox米数.Text = form已酸洗未轧硬.卷信息.OutputMeter.ToString();
            //    this.uiTextBox新厚度.Text = 厚度;
            //    this.uiTextBox新宽度.Text = 宽度;
            //    this.uiTextBox产地1.Text = form已酸洗未轧硬.卷信息.OutputAddress;
            //    this.uiTextBox卷重量1.Text = form已酸洗未轧硬.卷信息.OutputWeight.ToString();
            //    this.uiTextBox新重量.Text = this.uiTextBox卷重量1.Text;
            //    this.uiTextBox新米数.Text = this.uiTextBox米数.Text;
            //    this.uiTextBox材质1.Text = form已酸洗未轧硬.卷信息.OutputMaterial;
            //    this.uiComboBox新材质.Text = this.uiTextBox材质1.Text;
            //    this.uiTextBox计量单位1.Text = "Kg";
            //    uiTextBox成品规格.Text = form已酸洗未轧硬.计划卷信息.ProductSize;
            //    uiTextBox轧制规格.Text = form已酸洗未轧硬.计划卷信息.OutputSize;
            //    if (form已酸洗未轧硬.计划卷信息.NextWorkType != "")
            //    {
            //        uiTextBox计划去向.Text = form已酸洗未轧硬.计划卷信息.NextWorkType;
            //        this.uiComboBox去向.SelectedValue = form已酸洗未轧硬.计划卷信息.NextWorkType;
            //    }
            //    else
            //    {
            //        uiTextBox计划去向.Text = "";
            //    }

            //    ListOutput(this.uiTextBox原料卷号.Text.Trim().Replace("--", ""));
            //    塞入自己的tempguid的数据(tempguid, form已酸洗未轧硬.卷信息.VolumeCodeNew);
            //}
            //else
            //{
            //    轧硬原卷guid = "";
            //    删除自己的tempguid的数据(tempguid);  //Temp_轧硬报工原卷占用
            //}



        }

        private void 塞入自己的tempguid的数据(string tempguid, string volumeCodeNew)
        {
            string sql;
            sql = "insert into Temp_轧硬报工原卷占用(guid, OriginalVolumeNew, MAC, 时间 ) values('" + tempguid + "','" + volumeCodeNew + "','" + pub.getMac() + "' ,getdate()) ";
            pub.ExecuteSql(pub.ConnectionString, sql, "删除自己的tempguid的数据,Temp_轧硬报工原卷占用");
        }

        private void 删除自己的tempguid的数据(string tempguid)
        {
            string sql;
            //sql = "delete Temp_轧硬报工原卷占用 where guid='" + tempguid + "' ";
            sql = "delete Temp_轧硬报工原卷占用 where mac='" + pub.getMac() + "' ";
            pub.ExecuteSql(pub.ConnectionString, sql, "删除自己的tempguid的数据,Temp_轧硬报工原卷占用");
        }

        private void uiComboBoxWorkShop_SelectedIndexChanged(object sender, EventArgs e)
        {
            //CleallAll();

            pub.ListDayShift(pub.全局工厂编号, this.uiComboBoxDayShift);
            pub.ListWorkLine(pub.全局工厂编号, FormWorkType, this.uiComboBox产线);
            pub.ListRank(pub.全局工厂编号, this.uiComboBox质量等级);
            pub.ListDayShift(pub.全局工厂编号, this.uiComboBoxDayShift);
        }

        private void CleallAll()
        {
            //this.uiDataGridView产出.DataSource = null;
            //this.uiDataGridView产出.Refresh();

            //this.uiTextBox产地1.Text = "";
            //this.uiCheckBox拆成多卷.Checked = false;
            //this.numericUpDown1.Value = 1;
            //uiTextBox原料卷号.Text = "";
            //this.uiTextBox产出卷号.Text = "";
            //轧硬原卷guid = "";
            //删除自己的tempguid的数据(tempguid);

            //this.uiTextBox存货编号1.Text = "";
            //this.uiTextBox存货名称1.Text = "";
            //this.uiTextBox轧制规格.Text = "";
            //this.uiTextBox成品规格.Text = "";
            //this.uiTextBox备注1.Text = "";
            //uiTextBox规格型号1.Text = "";
            //string 厚度 = "";
            //string 宽度 = "";
            //string 虚拟值 = "";

            //this.uiTextBox厚度1.Text = 厚度;
            //this.uiTextBox宽度1.Text = 宽度;
            //this.uiTextBox虚拟值1.Text = 虚拟值;
            //this.uiTextBox新厚度.Text = 厚度;
            //this.uiTextBox新宽度.Text = 宽度;
            //this.uiTextBox产地1.Text = "";
            //this.uiTextBox卷重量1.Text = "";
            //this.uiTextBox材质1.Text = "";
            ////this.uiComboBox新材质.Text = "";
            //this.uiTextBox计量单位1.Text = "Kg";
            ////this.uiComboBox去向.SelectedValue = form已发未完工卷.出库卷信息.NextWorkType;


            //uiTextBox新重量.Text = "";
            //uiTextBox新规格.Text = "";
            //uiTextBox新重量.Text = "";
            //uiTextBox轧制规格.Text = "";
            tempguid = Guid.NewGuid().ToString();
            this.uiDatePicker报工日期.Value = DateTime.Now;
            清理自己mac的Temp_轧硬报工原卷占用();



        }

        private void uiButton选择部门1_Click(object sender, EventArgs e)
        {

            Form部门档案 form部门档案 = new Form部门档案();
            form部门档案.工厂编号 = pub.全局工厂编号;
            if (form部门档案.ShowDialog(this) == DialogResult.OK)
            {
                //this.uiTextBox部门1.Text = form部门档案.部门编号;
                //this.uiTextBox部门名称1.Text = form部门档案.部门名称;
                部门guid = form部门档案.Guid.ToString();
                // pub.ListWorkLine(form部门档案.工厂编号, this.uiTextBox部门1.Text, FormWorkType, this.uiTextBox产线搜索1.Text.Trim().Replace("--", ""), this.uiComboBox产线);
            }
        }




        private void uiComboBox产线_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                if (this.uiComboBox产线.SelectedValue.ToString() != "")
                {

                    pub.ListWorkType(pub.全局工厂编号, FormWorkType, this.uiComboBox去向);
                }
            }
            catch
            {

            }

        }

        private void uiTextBox新厚度_TextChanged(object sender, EventArgs e)
        {
            SetSize();
        }

        private void uiTextBox新宽度_TextChanged(object sender, EventArgs e)
        {
            SetSize();
        }
        private void SetSize()
        {
            this.uiTextBox新规格.Text = pub.Todecimal(this.uiTextBox新厚度.Text, 3) + "*" + pub.ToInt(this.uiTextBox新宽度.Text) + "*C";
        }

        private void uiCheckBox拆成多卷_CheckedChanged(object sender, EventArgs e)
        {
            if (this.uiCheckBox拆成多卷.Checked)
            {
                this.numericUpDown1.Visible = true;
            }
            else
            {
                this.numericUpDown1.Visible = false;
            }
        }

        private void uiButton添加产出卷_Click(object sender, EventArgs e)
        {
            if (pub.Todecimal(this.uiTextBox卷重量1.Text) * (pub.Todecimal(1.02)) < 卷的总重量 + pub.Todecimal(this.uiTextBox新重量.Text))
            {
                MessageBox.Show("产出卷的重量超过了投入卷重量,不能单卷报工。", "提示", MessageBoxButtons.OK);
                return;
            }

            if (this.uiComboBox质量等级.SelectedText == "全部")
            {
                MessageBox.Show("质量等级不能是全部。请选择具体的质量等级。", "提示", MessageBoxButtons.OK);
                return;
            }

            if (string.IsNullOrWhiteSpace(uiComboBox新材质.Text.Trim()))
            {
                MessageBox.Show("请选择或输入材质", "提示", MessageBoxButtons.OK);
                return;
            }

            if (this.uiCheckBox拆成多卷.Checked == false)
            {
                //单卷
                //已经存在的就不能再次点击添加了
                if (!pub.Is轧硬报工原始卷被关闭(this.uiTextBox原料卷号.Text))
                {
                    string sql;
                    List<Bus_HardOutput> bus_hardoutputs = pub.Get轧硬产出卷信息All(this.uiTextBox原料卷号.Text);
                    if (bus_hardoutputs != null)
                    {
                        MessageBox.Show("这个卷已经有产出卷了,不能单卷报工。", "提示", MessageBoxButtons.OK);
                        return;
                    }
                    //if (MessageBox.Show("这卷不再分拆会自动关闭，是否确认?", "询问", MessageBoxButtons.OKCancel) == DialogResult.OK)
                    //{
                    单卷报工();
                    //}
                }
                else
                {
                    MessageBox.Show("这个卷已经被关闭了，不能再次报工了，如需要再次报工，请联系管理员处理。", "提示", MessageBoxButtons.OK);
                    return;
                }

            }
            else
            {
                //单卷
                //已经存在的就不能再次点击添加了
                if (!pub.Is轧硬报工原始卷被关闭(this.uiTextBox原料卷号.Text))
                {
                    List<Bus_HardOutput> bus_hardoutputs = pub.Get轧硬产出卷信息All(this.uiTextBox原料卷号.Text);
                    if (bus_hardoutputs != null)
                    {
                        bool s = false;
                        foreach (Bus_HardOutput bus_hardoutput in bus_hardoutputs)
                        {
                            if (this.uiTextBox原料卷号.Text.Replace("PHS", "") == bus_hardoutput.OutputVolumeNew.Replace("PHY", ""))
                            {
                                s = true;
                            }
                        }
                        if (s)
                        {
                            MessageBox.Show("这个卷已经有产出单卷了,不能多卷报工。", "提示", MessageBoxButtons.OK);
                            return;
                        }

                    }
                    多卷报工();
                }
                else
                {
                    MessageBox.Show("这个卷已经被关闭了，不能再次报工了，如需要强制报工，请联系管理员处理。", "提示", MessageBoxButtons.OK);
                    return;
                }

            }
        }

        private void 多卷报工()
        {
            if (轧硬原卷guid == "" || this.uiTextBox原料卷号.Text == "")
            {
                MessageBox.Show("没有选择投入卷号", "提示", MessageBoxButtons.OK);
                return;
            }
            string sql;
            //在轧硬卷分卷时补0，比如01,02
            string 新卷号 = this.uiTextBox前缀.Text + this.uiTextBox产出卷号.Text + this.numericUpDown1.Value.ToString().PadLeft(2, '1');

            Bus_HardOutput bus_hardoutput = new Bus_HardOutput();
            bus_hardoutput.Creator = pub.sys_user.UserName;
            bus_hardoutput.OutputDate = this.uiDatePicker报工日期.Value;
            if (this.uiComboBox去向.SelectedText == "")
            {
                MessageBox.Show("去向不能为空", "提示", MessageBoxButtons.OK);
                return;

            }
            if (pub.Get轧硬产出卷信息(新卷号) != null)
            {
                MessageBox.Show("产出卷号已经存在了，不能添加", "提示", MessageBoxButtons.OK);
                return;
            }
            bus_hardoutput.OutputNext = this.uiComboBox去向.SelectedText;
            bus_hardoutput.WorkLineName = this.uiComboBox产线.SelectedText;
            if (bus_hardoutput.WorkLineName == "")
            {
                MessageBox.Show("产线为空，不能添加", "提示", MessageBoxButtons.OK);
                return;
            }
            if (!pub.IsRightSize(this.uiTextBox新规格.Text))
            {
                MessageBox.Show("规格有问题，不能添加", "提示", MessageBoxButtons.OK);
                return;
            }
            bus_hardoutput.WorkLineCode = this.uiComboBox产线.SelectedValue.ToString();
            bus_hardoutput.DayShiftCode = this.uiComboBoxDayShift.SelectedValue.ToString();
            bus_hardoutput.DayShiftName = this.uiComboBoxDayShift.SelectedText;

            bus_hardoutput.WorkTeamCode = this.uiComboBoxWorkTeam.SelectedText;
            bus_hardoutput.WorkTeamName = this.uiComboBoxWorkTeam.SelectedText;

            //bus_hardoutput.DepartCode = this.uiTextBox部门1.Text;
            if (bus_hardoutput.DepartCode == "")
            {
                MessageBox.Show("部门为空，不能添加", "提示", MessageBoxButtons.OK);
                return;
            }
            //bus_hardoutput.DepartName = this.uiTextBox部门名称1.Text;
            bus_hardoutput.guid = Guid.Parse(轧硬原卷guid);
            bus_hardoutput.OutputGuid = Guid.NewGuid();
            bus_hardoutput.OutputMaterial = this.uiComboBox新材质.Text;
            bus_hardoutput.OutputQualityGrade = this.uiComboBox质量等级.SelectedText;
            bus_hardoutput.OutputQualityInformation = this.uiComboBox质量信息.Text.Trim().Replace("--", "");
            pub.UpdateQualityInformation(pub.全局工厂编号, FormWorkType, bus_hardoutput.OutputQualityInformation);
            bus_hardoutput.OutputSize = this.uiTextBox新规格.Text;
            bus_hardoutput.OutputThick = pub.Todecimal(uiTextBox新厚度.Text);
            bus_hardoutput.OutputWidth = uiTextBox新宽度.Text;
            bus_hardoutput.OutputWeight = pub.Todecimal(uiTextBox新重量.Text);
            if (bus_hardoutput.OutputWeight == 0)
            {
                MessageBox.Show("重量为0，不能添加", "提示", MessageBoxButtons.OK);
                return;
            }
            bus_hardoutput.OutputMeter = 0;
            //bus_hardoutput.OutputMeter = pub.Todecimal(uiTextBox新米数.Text);
            //if (bus_hardoutput.OutputMeter == 0)
            //{
            //    MessageBox.Show("米数为0，不能添加", "提示", MessageBoxButtons.OK);
            //    return;
            //}
            bus_hardoutput.OutputAddress = this.uiTextBox产地1.Text;
            if (this.uiComboBox品名.Visible == true)
            {
                bus_hardoutput.InventoryName = uiComboBox品名.SelectedText;
                bus_hardoutput.InventoryCode = pub.GetInventoryCode(bus_hardoutput.InventoryName, bus_hardoutput.OutputSize, bus_hardoutput.OutputMaterial);
                if (bus_hardoutput.InventoryCode == "")
                {
                    MessageBox.Show("这个卷没有对应的NC存货编号", "提示", MessageBoxButtons.OK);
                }
            }
            bus_hardoutput.Customer = this.uiTextBox客户.Text.Trim().Replace("--", "");

            sql = " insert into Bus_HardOutput(OutputGuid,outputdate, guid, VolumeCodeNew,Row, OutputVolumeNew, OutputSize, OutputThick, OutputWidth, OutputMaterial, OutputWeight,";
            sql += " OutputMeter,OutputQualityGrade, OutputQualityInformation, OutputEndStatus, OutputNext, PrintTimes, DepartCode, DepartName, ";
            sql += " WorkLineCode, WorkLineName, WorkTeamCode, WorkTeamName, DayShiftCode, DayShiftName, CreateID, Creator,  CreateDate,OutputAddress,InventoryCode, InventoryName,Customer)";
            sql += " values(newid(),'" + bus_hardoutput.OutputDate + "','" + 轧硬原卷guid + "','" + this.uiTextBox原料卷号.Text.Trim().Replace("--", "") + "','" + this.numericUpDown1.Value.ToString() + "','" + 新卷号 + "','" + bus_hardoutput.OutputSize + "','" + bus_hardoutput.OutputThick + "','" + bus_hardoutput.OutputWidth + "',";
            sql += " '" + bus_hardoutput.OutputMaterial + "','" + bus_hardoutput.OutputWeight + "','" + bus_hardoutput.OutputMeter + "','" + bus_hardoutput.OutputQualityGrade + "','" + bus_hardoutput.OutputQualityInformation + "',";
            sql += "  '','" + bus_hardoutput.OutputNext + "',0,'" + bus_hardoutput.DepartCode + "','" + bus_hardoutput.DepartName + "',";
            sql += " '" + bus_hardoutput.WorkLineCode + "','" + bus_hardoutput.WorkLineName + "','" + bus_hardoutput.WorkTeamCode + "','" + bus_hardoutput.WorkTeamName + "',";
            sql += " '" + bus_hardoutput.DayShiftCode + "','" + bus_hardoutput.DayShiftName + "','" + pub.sys_user.User_Id + "','" + pub.sys_user.UserTrueName + "',";
            sql += " getdate(),'" + bus_hardoutput.OutputAddress + "','" + bus_hardoutput.InventoryCode + "','" + bus_hardoutput.InventoryName + "','" + bus_hardoutput.Customer + "') ";
            if (pub.ExecuteSql(pub.ConnectionString, sql, "多卷轧硬报工塞入bus_hardoutput") > 0)
            {
                MessageBox.Show("多卷轧硬报工成功", "提示", MessageBoxButtons.OK);
                this.numericUpDown1.Value = this.numericUpDown1.Value + 1;
                //不用自动关闭
                //sql = "update Bus_PicklingOriginal set IsClosed='Y' , ColseUserId='" + pub.sys_user.User_Id + "',ColseUserName='" + pub.sys_user.UserTrueName + "',CloseDate=getdate() where guid='" + 轧硬原卷guid + "'";
                //pub.ExecuteSql(pub.ConnectionString, sql, "单卷轧硬报工成功，自动关闭原卷的状态");
                //显示卷信息到表格上
                ListOutput(this.uiTextBox原料卷号.Text.Trim().Replace("--", ""));
                return;
            }
        }

        private void 单卷报工()
        {
            if (轧硬原卷guid == "" || this.uiTextBox原料卷号.Text == "")
            {
                MessageBox.Show("没有选择投入卷号", "提示", MessageBoxButtons.OK);
                return;
            }
            string sql;
            string 新卷号 = this.uiTextBox前缀.Text + this.uiTextBox产出卷号.Text;
            Bus_HardOutput bus_hardoutput = new Bus_HardOutput();
            bus_hardoutput.Creator = pub.sys_user.UserName;
            bus_hardoutput.OutputDate = this.uiDatePicker报工日期.Value;
            if (this.uiComboBox去向.SelectedText == "")
            {
                MessageBox.Show("去向不能为空", "提示", MessageBoxButtons.OK);
                return;

            }
            bus_hardoutput.OutputNext = this.uiComboBox去向.SelectedText;
            bus_hardoutput.WorkLineName = this.uiComboBox产线.SelectedText;
            if (bus_hardoutput.WorkLineName == "")
            {
                MessageBox.Show("产线为空，不能添加", "提示", MessageBoxButtons.OK);
                return;
            }
            if (!pub.IsRightSize(this.uiTextBox新规格.Text))
            {
                MessageBox.Show("规格有问题，不能添加", "提示", MessageBoxButtons.OK);
                return;
            }

            if (pub.Get轧硬产出卷信息(新卷号) != null)
            {
                MessageBox.Show("产出卷号已经存在了，不能添加", "提示", MessageBoxButtons.OK);
                return;
            }

            bus_hardoutput.WorkLineCode = this.uiComboBox产线.SelectedValue.ToString();
            bus_hardoutput.DayShiftCode = this.uiComboBoxDayShift.SelectedValue.ToString();
            bus_hardoutput.DayShiftName = this.uiComboBoxDayShift.SelectedText;

            bus_hardoutput.WorkTeamCode = this.uiComboBoxWorkTeam.SelectedText;
            bus_hardoutput.WorkTeamName = this.uiComboBoxWorkTeam.SelectedText;

            //bus_hardoutput.DepartCode = this.uiTextBox部门1.Text;
            //if (bus_hardoutput.DepartCode == "")
            //{
            //    MessageBox.Show("部门为空，不能添加", "提示", MessageBoxButtons.OK);
            //    return;
            //}
            //bus_hardoutput.DepartName = this.uiTextBox部门名称1.Text;
            bus_hardoutput.guid = Guid.Parse(轧硬原卷guid);
            bus_hardoutput.OutputGuid = Guid.NewGuid();
            bus_hardoutput.OutputMaterial = this.uiComboBox新材质.Text;
            bus_hardoutput.OutputQualityGrade = this.uiComboBox质量等级.SelectedText;
            bus_hardoutput.OutputQualityInformation = this.uiComboBox质量信息.Text.Trim().Replace("--", "");
            pub.UpdateQualityInformation(pub.全局工厂编号, FormWorkType, bus_hardoutput.OutputQualityInformation);
            bus_hardoutput.OutputSize = this.uiTextBox新规格.Text;
            bus_hardoutput.OutputThick = pub.Todecimal(uiTextBox新厚度.Text);
            bus_hardoutput.OutputWidth = uiTextBox新宽度.Text;
            bus_hardoutput.OutputWeight = pub.Todecimal(uiTextBox新重量.Text);
            if (bus_hardoutput.OutputWeight == 0)
            {
                MessageBox.Show("重量为0，不能添加", "提示", MessageBoxButtons.OK);
                return;
            }
            //bus_hardoutput.OutputMeter = pub.Todecimal(uiTextBox新米数.Text);
            //if (bus_hardoutput.OutputMeter == 0)
            //{
            //    MessageBox.Show("米数为0，不能添加", "提示", MessageBoxButtons.OK);
            //    return;
            //}
            bus_hardoutput.OutputMeter = 0;
            bus_hardoutput.OutputAddress = this.uiTextBox产地1.Text;
            if (this.uiComboBox品名.Visible == true)
            {
                bus_hardoutput.InventoryName = uiComboBox品名.SelectedText;
                bus_hardoutput.InventoryCode = pub.GetInventoryCode(bus_hardoutput.InventoryName, bus_hardoutput.OutputSize, bus_hardoutput.OutputMaterial);
                if (bus_hardoutput.InventoryCode == "")
                {
                    MessageBox.Show("这个卷没有对应的NC存货编号", "提示", MessageBoxButtons.OK);
                }
            }
            bus_hardoutput.Customer = this.uiTextBox客户.Text.Trim().Replace("--", "");
            sql = " insert into Bus_HardOutput(OutputGuid,outputdate, guid, VolumeCodeNew,Row, OutputVolumeNew, OutputSize, OutputThick, OutputWidth, OutputMaterial, OutputWeight,";
            sql += " OutputMeter,OutputQualityGrade, OutputQualityInformation, OutputEndStatus, OutputNext, PrintTimes, DepartCode, DepartName, ";
            sql += " WorkLineCode, WorkLineName, WorkTeamCode, WorkTeamName, DayShiftCode, DayShiftName, CreateID, Creator,  CreateDate,OutputAddress,InventoryCode, InventoryName,Customer)";
            sql += " values(newid(),'" + bus_hardoutput.OutputDate + "','" + 轧硬原卷guid + "','" + this.uiTextBox原料卷号.Text.Trim().Replace("--", "") + "','" + this.numericUpDown1.Value.ToString() + "','" + 新卷号 + "','" + bus_hardoutput.OutputSize + "','" + bus_hardoutput.OutputThick + "','" + bus_hardoutput.OutputWidth + "',";
            sql += " '" + bus_hardoutput.OutputMaterial + "','" + bus_hardoutput.OutputWeight + "','" + bus_hardoutput.OutputMeter + "','" + bus_hardoutput.OutputQualityGrade + "','" + bus_hardoutput.OutputQualityInformation + "',";
            sql += "  '','" + bus_hardoutput.OutputNext + "',0,'" + bus_hardoutput.DepartCode + "','" + bus_hardoutput.DepartName + "',";
            sql += " '" + bus_hardoutput.WorkLineCode + "','" + bus_hardoutput.WorkLineName + "','" + bus_hardoutput.WorkTeamCode + "','" + bus_hardoutput.WorkTeamName + "',";
            sql += " '" + bus_hardoutput.DayShiftCode + "','" + bus_hardoutput.DayShiftName + "','" + pub.sys_user.User_Id + "','" + pub.sys_user.UserTrueName + "',";
            sql += " getdate(),'" + bus_hardoutput.OutputAddress + "','" + bus_hardoutput.InventoryCode + "','" + bus_hardoutput.InventoryName + "','" + bus_hardoutput.Customer + "') ";
            if (pub.ExecuteSql(pub.ConnectionString, sql, "单卷轧硬报工塞入bus_hardoutput") > 0)
            {
                MessageBox.Show("单卷轧硬报工成功", "提示", MessageBoxButtons.OK);
                //sql = "update Bus_HardOriginal set IsClosed='Y' , ColseUserId='" + pub.sys_user.User_Id + "',ColseUserName='" + pub.sys_user.UserTrueName + "',CloseDate=getdate() where guid='" + 轧硬原卷guid + "'";
                //pub.ExecuteSql(pub.ConnectionString, sql, "单卷轧硬报工成功，自动关闭原卷的状态");
                //显示卷信息到表格上
                ListOutput(this.uiTextBox原料卷号.Text.Trim().Replace("--", ""));
                return;
            }
        }

        private void ListOutput(string 投入卷号)
        {
            卷的总重量 = 0;
            string 查询投入卷号 = "PHR" + 投入卷号.Replace("PHR", "");  //只查卷号的7位纯数字
            string sql;
            sql = "SELECT   OutputGuid,outputdate 报工日期, guid, Row, OutputVolumeNew 新卷号, OutputSize 新规格, OutputThick 新厚度, OutputWidth 新宽度, OutputMaterial 新材质, OutputWeight 新重量,";
            sql += " OutputMeter 新米数,OutputQualityGrade 质量等级, OutputQualityInformation 质量信息, OutputEndStatus 后续状态, OutputNext 去向,InventoryCode 存货编号,InventoryName 品名,";
            sql += " PrintTimes 打印次数, DepartCode 部门编号, DepartName 生产部门,  WorkLineCode 产线编号, WorkLineName 产线, WorkTeamCode 班组编号, WorkTeamName 班组, DayShiftCode 班别编号,";
            sql += " DayShiftName 班别, CreateID, Creator 填报人,  CreateDate 填报时间, ModifyID, Modifier, ModifyDate,Customer 客户  ";
            sql += " FROM      bus_hardoutput where VolumeCodeNew='" + 查询投入卷号 + "' order by OutputVolumeNew ";
            DataTable tb = pub.GetDataTable(pub.ConnectionString, sql);
            if (tb != null)
            {
                uiDataGridView产出.AutoGenerateColumns = false;
                this.uiDataGridView产出.DataSource = tb;
                this.uiDataGridView产出.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells;
                this.uiDataGridView产出.Refresh();
                //产出卷必须连号,不连号不行。
                for (int i = 0; i < this.uiDataGridView产出.RowCount; i++)
                {
                    if (this.uiDataGridView产出.Rows[i].Cells["C质量等级"].Value.ToString() == "B")
                    {
                        this.uiDataGridView产出.Rows[i].Cells["C质量等级"].Style.BackColor = Color.FromArgb(255, 255, 128);
                    }
                    else if (this.uiDataGridView产出.Rows[i].Cells["C质量等级"].Value.ToString() == "S")
                    {
                        this.uiDataGridView产出.Rows[i].Cells["C质量等级"].Style.BackColor = Color.FromArgb(255, 0, 0);
                    }
                }

                if (tb.Rows.Count > 0)
                {
                    if (tb.Rows.Count == 1)
                    {
                        //只有一卷
                        卷的总重量 = pub.Todecimal(tb.Rows[0]["新重量"].ToString());
                    }
                    else
                    {
                        for (int i = 0; i < tb.Rows.Count; i++)
                        {
                            卷的总重量 += pub.Todecimal(tb.Rows[i]["新重量"].ToString());


                        }
                    }


                }
            }


            //显示是否关闭了。
            sql = "select IsClosed from [Bus_HardOriginal] where OriginalVolumeNew='" + 投入卷号 + "' ";
            DataTable tb2 = pub.GetDataTable(pub.ConnectionString, sql);
            if (tb2 != null)
            {
                if (tb2.Rows.Count > 0)
                {
                    if (tb2.Rows[0]["IsClosed"].ToString() == "Y")
                    {
                        this.uiLabelIsClosed.Text = "已关闭";
                    }
                    else
                    {
                        this.uiLabelIsClosed.Text = "未关闭";
                    }
                }
            }

        }

        private void Form轧硬报工维护_FormClosed(object sender, FormClosedEventArgs e)
        {
            删除自己的tempguid的数据(tempguid);
        }

        private void uiDataGridView产出_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex >= 0 && this.uiDataGridView产出.Rows.Count > 0)
            {

                if (this.uiDataGridView产出.Columns[e.ColumnIndex].Name == "C删除")
                {
                    if (!pub.Is轧硬报工原始卷被关闭(this.uiTextBox原料卷号.Text))
                    {
                        string 轧硬产出卷号 = this.uiDataGridView产出.Rows[e.RowIndex].Cells["C新卷号"].Value.ToString();
                        string 后续状态 = pub.Get轧硬产出卷的后续状态(轧硬产出卷号);
                        if (后续状态 != "未使用")
                        {
                            MessageBox.Show("这个卷状态:" + 后续状态, "提示", MessageBoxButtons.OK);
                            //显示卷信息到表格上
                            // this.uiDataGridView产出.Rows[e.RowIndex].Cells["C后续状态"].Value = 后续状态;
                            ListOutput(this.uiTextBox原料卷号.Text.Trim().Replace("--", ""));
                            return;
                        }
                        else
                        {   //本人才能删除
                            string 填报人 = this.uiDataGridView产出.Rows[e.RowIndex].Cells["C填报人"].Value.ToString();
                            if (填报人 != pub.sys_user.UserTrueName)
                            {
                                MessageBox.Show("您不可以删除其他人员填报的数据", "提示", MessageBoxButtons.OK);
                                return;
                            }
                            if (MessageBox.Show("你要删除这个产出卷么?", "询问", MessageBoxButtons.OKCancel) == DialogResult.OK)
                            {
                                //检查是否打印打标签了
                                if (pub.产出卷是否打印入库(轧硬产出卷号))
                                {
                                    MessageBox.Show("【" + 轧硬产出卷号 + "】已经打印大标签入库，无法删除");
                                    return;
                                }

                                string sql = "delete bus_hardoutput where OutputVolumeNew='" + 轧硬产出卷号 + "';";
                                sql += "delete from Bus_LablePrint where 生产批号 = '" + 轧硬产出卷号 + "';";
                                pub.ExecuteSql(pub.ConnectionString, sql, "删除产出的轧硬卷");
                                //原卷的状态还原
                                sql = "update Bus_HardOriginal set IsClosed='N' , ColseUserId=NULL,ColseUserName=NULL,CloseDate=NULL where guid='" + 轧硬原卷guid + "'";
                                pub.ExecuteSql(pub.ConnectionString, sql, "单卷轧硬报工取消成功，自动开启原卷的状态");

                                ListOutput(this.uiTextBox原料卷号.Text.Trim().Replace("--", ""));
                            }

                        }
                    }

                    else
                    {
                        MessageBox.Show("已经关闭的投入卷不能删除产出卷信息", "提示", MessageBoxButtons.OK);
                        ListOutput(this.uiTextBox原料卷号.Text.Trim().Replace("--", ""));
                        return;
                    }

                }
                else if (this.uiDataGridView产出.Columns[e.ColumnIndex].Name == "C打印小标签" && this.uiDataGridView产出.Rows[e.RowIndex].Cells["C去向"].Value.ToString() == "下线入库")
                {

                    Form小标签 form小标签 = new Form小标签();
                    form小标签.生产批号 = this.uiDataGridView产出.Rows[e.RowIndex].Cells["C新卷号"].Value.ToString();
                    form小标签.规格 = this.uiDataGridView产出.Rows[e.RowIndex].Cells["C新规格"].Value.ToString();
                    form小标签.钢号 = this.uiDataGridView产出.Rows[e.RowIndex].Cells["C新材质"].Value.ToString();
                    form小标签.等级 = this.uiDataGridView产出.Rows[e.RowIndex].Cells["C质量等级"].Value.ToString();
                    form小标签.净重 = Math.Round(pub.Todecimal(this.uiDataGridView产出.Rows[e.RowIndex].Cells["C新重量"].Value.ToString()), 0).ToString();
                    form小标签.工厂编号 = pub.全局工厂编号;
                    form小标签.来源种类 = "轧硬小标签";
                    form小标签.存货编号 = this.uiDataGridView产出.Rows[e.RowIndex].Cells["C存货编号"].Value.ToString();
                    form小标签.品名 = this.uiDataGridView产出.Rows[e.RowIndex].Cells["C品名"].Value.ToString();
                    form小标签.表面处理 = "";
                    form小标签.表面状态 = "";
                    form小标签.执行标准 = this.uiComboBox执行标准.SelectedText;
                    form小标签.长度 = "";
                    form小标签.班组 = this.uiComboBoxWorkTeam.SelectedText;
                    form小标签.锌层 = this.uiTextBox锌层.Text;
                    form小标签.颜色 = this.uiTextBox颜色.Text;
                    form小标签.包装 = this.uiComboBox包装.SelectedText;
                    form小标签.仓库 = this.uiComboBox仓库.SelectedValue.ToString();  ///使用的是编号
                    form小标签.客户 = this.uiDataGridView产出.Rows[e.RowIndex].Cells["C客户"].Value.ToString();
                    form小标签.生产日期 = this.uiDataGridView产出.Rows[e.RowIndex].Cells["C报工日期"].Value.ToString();
                    form小标签.去向 = this.uiDataGridView产出.Rows[e.RowIndex].Cells["C去向"].Value.ToString();
                    //form小标签.ShowDialog(this);
                    form小标签.打印();
                }
                else if (this.uiDataGridView产出.Columns[e.ColumnIndex].Name == "C打印小标签" && this.uiDataGridView产出.Rows[e.RowIndex].Cells["C去向"].Value.ToString() == "镀锌")
                {
                    PrintUtil.打印("轧硬小标签", this.uiDataGridView产出.Rows[e.RowIndex].Cells["C新卷号"].Value.ToString(),
                        this.uiDataGridView产出.Rows[e.RowIndex].Cells["C品名"].Value.ToString(),
                        this.uiDataGridView产出.Rows[e.RowIndex].Cells["C新材质"].Value.ToString(),
                        "",
                        this.uiDataGridView产出.Rows[e.RowIndex].Cells["C新规格"].Value.ToString(),
                        this.uiDataGridView产出.Rows[e.RowIndex].Cells["C质量等级"].Value.ToString(),
                         "",
                         "",
                         Math.Round(pub.Todecimal(this.uiDataGridView产出.Rows[e.RowIndex].Cells["C新重量"].Value.ToString()), 0).ToString(),
                         this.uiComboBox执行标准.SelectedText,
                         this.uiDataGridView产出.Rows[e.RowIndex].Cells["C报工日期"].Value.ToString(),
                         this.uiDataGridView产出.Rows[e.RowIndex].Cells["C班组"].Value.ToString(),
                         this.uiDataGridView产出.Rows[e.RowIndex].Cells["C去向"].Value.ToString());
                }


            }
        }

        private void uiButton关闭_Click(object sender, EventArgs e)
        {
            if (this.uiTextBox原料卷号.Text != "")
            {
                string sql;
                //产出卷必须连号
                sql = "SELECT   OutputGuid, guid, Row, OutputVolumeNew 新卷号, OutputSize 新规格, OutputThick 新厚度, OutputWidth 新宽度, OutputMaterial 新材质, OutputWeight 新重量, OutputQualityGrade 质量等级, OutputQualityInformation 质量信息, OutputEndStatus 后续状态, OutputNext 去向,";
                sql += " PrintTimes 打印次数, DepartCode 部门编号, DepartName 生产部门,  WorkLineCode 产线编号, WorkLineName 产线, WorkTeamCode 班组编号, WorkTeamName 班组, DayShiftCode 班别编号, DayShiftName 班别, CreateID, Creator 填报人,  CreateDate 填报时间, ModifyID, Modifier, ModifyDate ";
                sql += " FROM      bus_hardoutput where guid='" + 轧硬原卷guid + "' order by OutputVolumeNew ";
                DataTable tb = pub.GetDataTable(pub.ConnectionString, sql);
                if (tb != null)
                {
                    uiDataGridView产出.AutoGenerateColumns = false;
                    this.uiDataGridView产出.DataSource = tb;
                    this.uiDataGridView产出.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells;
                    this.uiDataGridView产出.Refresh();
                    //产出卷必须连号,不连号不行。
                    if (tb.Rows.Count > 0)
                    {
                        if (tb.Rows.Count == 1)
                        {
                            //只有一卷
                        }
                        else
                        {
                            for (int i = 0; i < tb.Rows.Count; i++)
                            {
                                if (this.uiTextBox前缀.Text + uiTextBox产出卷号.Text + (i + 1).ToString().PadLeft(2, '1') != tb.Rows[i]["新卷号"].ToString())
                                {
                                    MessageBox.Show("产出的卷号不连号请检查", "提示", MessageBoxButtons.OK);
                                    return;
                                }
                            }
                        }

                        if (MessageBox.Show("你要关闭这个投入卷么?", "询问", MessageBoxButtons.OKCancel) == DialogResult.OK)
                        {
                            sql = "update Bus_HardOriginal set IsClosed='Y' , ColseUserId='" + pub.sys_user.User_Id + "',ColseUserName='" + pub.sys_user.UserTrueName + "',CloseDate=getdate() where guid='" + 轧硬原卷guid + "'";
                            pub.ExecuteSql(pub.ConnectionString, sql, "轧硬手动关闭原卷的状态");
                            ListOutput(this.uiTextBox原料卷号.Text.Trim().Replace("--", ""));
                        }
                    }

                }
            }
        }




        private void uiButton搜索已生产卷_Click(object sender, EventArgs e)
        {
            List已生产卷();
        }

        private void List已生产卷()
        {

            轧硬原卷guid = pub.岳阳Get轧硬报工guid(this.uiTextBox原料卷号.Text);
            this.uiTextBox客户.Text = "";
            Bus_HardOriginal bus_hardoriginal = pub.岳阳Get轧硬投入信息(pub.全局工厂编号, this.uiTextBox原料卷号.Text);
            if (bus_hardoriginal != null)
            {
                轧硬原卷guid = bus_hardoriginal.guid.ToString();
                this.uiTextBox产出卷号.Text = pub.去掉前缀(this.uiTextBox原料卷号.Text);//不含前缀，不含后面的串号
                uiTextBox规格型号1.Text = bus_hardoriginal.OriginalSize;
                string 投入厚度 = "";
                string 投入宽度 = "";

                string 成品厚度 = "";
                string 成品宽度 = "";

                string 虚拟值 = "";
                pub.FromSize(uiTextBox规格型号1.Text, out 投入厚度, out 投入宽度, out 虚拟值);
                Bus_PlanHard bus_PlanHard = pub.Get轧硬计划(this.uiTextBox原料卷号.Text);

                this.uiTextBox厚度1.Text = 投入厚度;
                this.uiTextBox宽度1.Text = 投入宽度;
                if (bus_PlanHard != null)
                {
                    pub.FromSize(bus_PlanHard.OutputSize, out 成品厚度, out 成品宽度, out 虚拟值); //轧硬的厚度来自计划单的轧制规格（可以改）
                    this.uiTextBox产地1.Text = bus_PlanHard.Address;
                    this.uiTextBox新厚度.Text = 成品厚度;
                    this.uiTextBox新宽度.Text = 投入宽度;
                    this.uiTextBox客户.Text = bus_PlanHard.Customer;  ///轧硬计划
                }
                else
                {
                    //this.uiTextBox新厚度.Text = "";
                    //this.uiTextBox新宽度.Text = "";

                }
                if (this.uiTextBox客户.Text == "")
                {
                    Bus_PicklingOutput bus_picklingoutput = pub.Get酸洗产出卷信息(this.uiTextBox原料卷号.Text);
                    if (bus_picklingoutput != null)
                    {
                        this.uiTextBox客户.Text = bus_picklingoutput.Customer; //酸洗产出
                    }
                }
                this.uiTextBox卷重量1.Text = (bus_hardoriginal.OriginalWeight * 1000).ToString();
                this.uiTextBox材质1.Text = bus_hardoriginal.OriginalMaterial.Replace("SPHC", "SPCC-1B");
                this.uiTextBox米数.Text = bus_hardoriginal.OriginalMeter.ToString();
                this.uiComboBox新材质.Text = this.uiTextBox材质1.Text;

                this.uiTextBox计量单位1.Text = "Kg";

                ListOutput(this.uiTextBox原料卷号.Text.Trim().Replace("--", ""));
                塞入自己的tempguid的数据(tempguid, bus_hardoriginal.OriginalVolumeNew);
            }
            else
            {
                MessageBox.Show("没有投入的卷信息，请先选择投入卷 " + this.uiTextBox原料卷号.Text, "提示", MessageBoxButtons.OK);
                return;
            }
        }


        //原 轧硬已生产卷
        //private void List已生产卷()
        //{

        //    轧硬原卷guid = pub.Get轧硬报工guid(this.uiTextBox原料卷号.Text);
        //    this.uiTextBox客户.Text = "";
        //    Bus_HardOriginal bus_hardoriginal = pub.Get轧硬投入信息(pub.全局工厂编号, this.uiTextBox原料卷号.Text);
        //    if (bus_hardoriginal != null)
        //    {
        //        轧硬原卷guid = bus_hardoriginal.guid.ToString();
        //        this.uiTextBox产出卷号.Text = pub.去掉前缀(this.uiTextBox原料卷号.Text);//不含前缀，不含后面的串号
        //        uiTextBox规格型号1.Text = bus_hardoriginal.OriginalSize;
        //        string 投入厚度 = "";
        //        string 投入宽度 = "";

        //        string 成品厚度 = "";
        //        string 成品宽度 = "";

        //        string 虚拟值 = "";
        //        pub.FromSize(uiTextBox规格型号1.Text, out 投入厚度, out 投入宽度, out 虚拟值);
        //        Bus_PlanHard bus_PlanHard = pub.Get轧硬计划(this.uiTextBox原料卷号.Text);

        //        this.uiTextBox厚度1.Text = 投入厚度;
        //        this.uiTextBox宽度1.Text = 投入宽度;
        //        if (bus_PlanHard != null)
        //        {
        //            pub.FromSize(bus_PlanHard.OutputSize, out 成品厚度, out 成品宽度, out 虚拟值); //轧硬的厚度来自计划单的轧制规格（可以改）
        //            this.uiTextBox产地1.Text = bus_PlanHard.Address;
        //            this.uiTextBox新厚度.Text = 成品厚度;
        //            this.uiTextBox新宽度.Text = 投入宽度;
        //            this.uiTextBox客户.Text = bus_PlanHard.Customer;  ///轧硬计划
        //        }
        //        else
        //        {
        //            //this.uiTextBox新厚度.Text = "";
        //            //this.uiTextBox新宽度.Text = "";

        //        }
        //        if (this.uiTextBox客户.Text == "")
        //        {
        //            Bus_PicklingOutput bus_picklingoutput = pub.Get酸洗产出卷信息(this.uiTextBox原料卷号.Text);
        //            if (bus_picklingoutput != null)
        //            {
        //                this.uiTextBox客户.Text = bus_picklingoutput.Customer; //酸洗产出
        //            }
        //        }




        //        this.uiTextBox卷重量1.Text = bus_hardoriginal.OriginalWeight.ToString();
        //        this.uiTextBox材质1.Text = bus_hardoriginal.OriginalMaterial.Replace("SPHC", "SPCC-1B");
        //        this.uiTextBox米数.Text = bus_hardoriginal.OriginalMeter.ToString();
        //        this.uiComboBox新材质.Text = this.uiTextBox材质1.Text;

        //        this.uiTextBox计量单位1.Text = "Kg";

        //        ListOutput(this.uiTextBox原料卷号.Text.Trim().Replace("--", ""));
        //        塞入自己的tempguid的数据(tempguid, bus_hardoriginal.OriginalVolumeNew);
        //    }
        //    else
        //    {
        //        MessageBox.Show("没有投入的卷信息，请先选择投入卷 " + this.uiTextBox原料卷号.Text, "提示", MessageBoxButtons.OK);
        //        return;
        //    }

        //}

        private void uiButtonClearAll_Click(object sender, EventArgs e)
        {
            CleallAll();
        }

        private void uiLabel42_Click(object sender, EventArgs e)
        {

        }

        private void uiLabel43_Click(object sender, EventArgs e)
        {

        }

        private void uiComboBox去向_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (this.uiComboBox去向.SelectedText == "下线入库")
            {
                //this.uiLabel表面处理.Visible = true;
                //this.uiComboBox执行标准.Visible = true;
                //this.uiLabel执行标准.Visible = true;
                //this.uiComboBox表面处理.Visible = true;
                this.uiLabel品名.Visible = true;
                this.uiComboBox品名.Visible = true;
                this.uiLabel仓库.Visible = true;
                this.uiComboBox仓库.Visible = true;
                this.uiLabel包装.Visible = true;
                //this.uiLabel锌层.Visible = true;
                //this.uiTextBox锌层.Visible = true;
                //this.uiLabel颜色.Visible = true;
                //this.uiTextBox颜色.Visible = true;
                uiComboBox包装.Visible=true;
            }
            else
            {
                this.uiLabel表面处理.Visible = false;
                this.uiComboBox执行标准.Visible = false;
                this.uiLabel执行标准.Visible = false;
                this.uiComboBox表面处理.Visible = false;
                this.uiLabel仓库.Visible = false;
                this.uiComboBox仓库.Visible = false;
                this.uiLabel品名.Visible = false;
                this.uiComboBox品名.Visible = false;
                uiComboBox包装.Visible = false;
                this.uiLabel包装.Visible = false;
                this.uiLabel锌层.Visible = false;
                this.uiTextBox锌层.Visible = false;
                this.uiLabel颜色.Visible = false;
                this.uiTextBox颜色.Visible = false;
            }
        }

        private void uiDataGridView产出_CellEndEdit(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex >= 0)
            {
                DataGridViewRow dgbr = this.uiDataGridView产出.Rows[e.RowIndex];


                string 规格 = dgbr.Cells["C新规格"].Value.ToString();
                string 厚度;

                string 宽度;
                string 虚拟值;
                pub.FromSize(规格, out 厚度, out 宽度, out 虚拟值);
                string 报工日期 = dgbr.Cells["C报工日期"].Value.ToString();
                string 填报人 = dgbr.Cells["C填报人"].Value.ToString();
                string 轧硬产出卷号 = this.uiDataGridView产出.Rows[e.RowIndex].Cells["C新卷号"].Value.ToString();
                decimal 重量 = pub.Todecimal(dgbr.Cells["C新重量"].Value.ToString());
                string 材质 = dgbr.Cells["C新材质"].Value.ToString();
                string 质量等级 = dgbr.Cells["C质量等级"].Value.ToString();
                string 去向 = dgbr.Cells["C去向"].Value.ToString();
                string 客户 = dgbr.Cells["C客户"].Value.ToString();
                string 质量信息 = dgbr.Cells["C质量信息"].Value.ToString();
                string 存货编号 = dgbr.Cells["C存货编号"].Value.ToString();
                string 品名 = dgbr.Cells["C品名"].Value.ToString();
                //string 表面处理 = dgbr.Cells["C表面处理"].Value.ToString();
                string 后续状态 = pub.Get轧硬产出卷的后续状态(轧硬产出卷号);
                if (后续状态 != "未使用")
                {
                    MessageBox.Show("这个卷状态:" + 后续状态, "提示", MessageBoxButtons.OK);
                    //显示卷信息到表格上
                    // this.uiDataGridView产出.Rows[e.RowIndex].Cells["C后续状态"].Value = 后续状态;
                    ListOutput(this.uiTextBox原料卷号.Text);
                    return;
                }
                else
                {   //本人才能删除

                    if (填报人 != pub.sys_user.UserTrueName && pub.sys_user.UserTrueName != "超级管理员")
                    {
                        MessageBox.Show("您不可以修改其他人员填报的数据", "提示", MessageBoxButtons.OK);
                        return;
                    }
                    if (MessageBox.Show("你要修改这个产出卷么？数据需要填正确哦！", "询问", MessageBoxButtons.OKCancel) == DialogResult.OK)
                    {
                        string sql = "update Bus_HardOutput set  " +
                            "OutputDate='" + 报工日期 + "'," +
                            "OutputSize='" + 规格 + "'," +
                            "OutputThick='" + 厚度 + "'," +
                            "OutputWidth='" + 宽度 + "'," +
                            "OutputQualityGrade='" + 质量等级 + "'," +
                            "OutputMaterial='" + 材质 + "'," +
                            "OutputNext='" + 去向 + "'," +
                            "OutputWeight='" + 重量 + "'," +
                            "Customer='" + 客户 + "'," +
                            "OutputQualityInformation='" + 质量信息 + "'," +
                            "InventoryCode='" + 存货编号 + "'," +
                            "InventoryName='" + 品名 + "'" +
                            //"FaceDone='" + 表面处理 + "'" +
                            "where OutputVolumeNew='" + 轧硬产出卷号 + "'";
                        if (pub.ExecuteSql(pub.ConnectionString, sql, "修改产出的轧硬卷") > 0)
                        {
                            MessageBox.Show("修改成功", "提示", MessageBoxButtons.OK);
                        }
                        else
                        {
                            MessageBox.Show("修改失败", "提示", MessageBoxButtons.OK);
                        }
                    }

                }

            }
        }

        private void uiButton查询_Click(object sender, EventArgs e)
        {
            //List待轧硬清单();
            List待酸轧清单();
        }

        private void List待酸轧清单()
        {
            string sql;
            sql = @"SELECT row_number() over(order by  计划日期,卷号 ) 序号,   工厂编号, 仓库编号, 库位编号, 存货编号, 卷号, 轧制规格, 成品规格, 去向, 材质,产地, 计划日期, 产线编号, 产线名称, 规格型号, 
                重量, 备注,客户 FROM      View_已发未完工卷 where 1=1 ";
            sql += " and 工厂编号='" + pub.全局工厂编号 + "' ";
            if (this.uiTextBox查询材质.Text != "")
            {
                sql += " and 材质 ='" + this.uiTextBox查询材质.Text + "'";
            }
            if (this.uiTextBox查询规格型号.Text.Trim() != "")
            {
                sql += " and 规格型号 ='" + this.uiTextBox查询规格型号.Text.Trim().Replace("--", "") + "'";
            }

            if (this.uiTextBox查询卷号.Text.Trim() != "")
            {
                sql += " and 卷号 like '%" + this.uiTextBox查询卷号.Text.Trim().Replace("--", "") + "%'";
            }
            if (this.uiComboBox查询去向.SelectedText.Trim() != "全部" && this.uiComboBox查询去向.SelectedText.Trim() != "")
            {
                sql += " and 去向 ='" + this.uiComboBox查询去向.SelectedText.Trim().Replace("--", "") + "'";
            }

            if (this.uiTextBox查询成品规格.Text.Trim() != "")
            {
                sql += " and 成品规格 ='" + this.uiTextBox查询成品规格.Text.Trim().Replace("--", "") + "'";
            }
            if (this.uiTextBox查询客户.Text.Trim() != "")
            {
                sql += " and 客户 ='" + this.uiTextBox查询客户.Text.Trim().Replace("--", "") + "'";
            }
            // sql += " and 产线名称='"+ this.uiComboBox产线.SelectedText +"'";


            DataTable tb = pub.GetDataTable(pub.ConnectionString, sql);
            if (tb.Rows.Count > 0)
            {

                this.uiDataGridView可用.AutoGenerateColumns = false;
                this.uiDataGridView可用.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells;
                this.uiDataGridView可用.DataSource = tb;
                this.uiDataGridView可用.Refresh();
            }
            else
            {
                //this.uiDataGridView已发未结束.AutoGenerateColumns = false;
                //this.uiDataGridView已发未结束.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells;
                this.uiDataGridView可用.DataSource = null;
                this.uiDataGridView可用.Refresh();
                MessageBox.Show("没有已发料未结束的卷可以选择", "提示", MessageBoxButtons.OK);
                return;

            }
        }

        private void List待轧硬清单()
        {
            string sql;
            sql = @"SELECT row_number() over(order by   卷号 )  序号,   卷号, 酸洗产出规格, 计划轧制规格,材质,产地, 重量, 米数, 质量等级, 质量信息,成品规格,计划去向, 
                重量, 备注,客户 FROM      View_已酸洗未轧硬   where 1=1  ";
            sql += " and 工厂编号='" + pub.全局工厂编号 + "' ";
            if (this.uiTextBox查询材质.Text != "")
            {
                sql += " and 材质 ='" + this.uiTextBox查询材质.Text + "'";
            }
            if (this.uiTextBox查询规格型号.Text.Trim() != "")
            {
                sql += " and 规格型号 ='" + this.uiTextBox查询规格型号.Text.Trim().Replace("--", "") + "'";
            }

            if (this.uiTextBox查询卷号.Text.Trim() != "")
            {
                sql += " and 卷号 like '%" + this.uiTextBox查询卷号.Text.Trim().Replace("--", "") + "%'";
            }
            if (this.uiComboBox查询去向.SelectedText.Trim() != "全部" && this.uiComboBox查询去向.SelectedText.Trim() != "")
            {
                sql += " and 去向 ='" + this.uiComboBox查询去向.SelectedText.Trim().Replace("--", "") + "'";
            }

            if (this.uiTextBox查询成品规格.Text.Trim() != "")
            {
                sql += " and 成品规格 ='" + this.uiTextBox查询成品规格.Text.Trim().Replace("--", "") + "'";

            }
            if (this.uiTextBox查询客户.Text.Trim() != "")
            {
                sql += " and 客户 ='" + this.uiTextBox查询客户.Text.Trim().Replace("--", "") + "'";
            }



            DataTable tb = pub.GetDataTable(pub.ConnectionString, sql);
            if (tb.Rows.Count > 0)
            {

                this.uiDataGridView可用.AutoGenerateColumns = false;
                this.uiDataGridView可用.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells;
                this.uiDataGridView可用.DataSource = tb;
                this.uiDataGridView可用.Refresh();
                for (int i = 0; i < this.uiDataGridView可用.RowCount; i++)
                {
                    if (this.uiDataGridView可用.Rows[i].Cells["D质量等级"].Value.ToString() == "B")
                    {
                        this.uiDataGridView可用.Rows[i].Cells["D质量等级"].Style.BackColor = Color.FromArgb(255, 255, 128);
                    }
                    else if (this.uiDataGridView可用.Rows[i].Cells["D质量等级"].Value.ToString() == "S")
                    {
                        this.uiDataGridView可用.Rows[i].Cells["D质量等级"].Style.BackColor = Color.FromArgb(255, 0, 0);
                    }
                }
            }
            else
            {
                //this.uiDataGridView已发未结束.AutoGenerateColumns = false;
                //this.uiDataGridView已发未结束.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells;
                this.uiDataGridView可用.DataSource = null;
                this.uiDataGridView可用.Refresh();
                MessageBox.Show("没有可以用来轧硬的卷", "提示", MessageBoxButtons.OK);
                return;

            }
        }

        private void uiDataGridView可用_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex >= 0 && this.uiDataGridView可用.Columns[e.ColumnIndex].Name == "D选择")
            {
                this.uiTextBox原料卷号.Text = this.uiDataGridView可用.Rows[e.RowIndex].Cells["D卷号"].Value.ToString();

                List已生产卷();
            }
        }

        private void uiComboBox质量等级_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void uiButton产出查询_Click(object sender, EventArgs e)
        {
            DataTable tb = ListData();
            uiDataGridView1.AutoGenerateColumns = false;
            this.uiDataGridView1.DataSource = tb;
            this.uiDataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells;
            this.uiDataGridView1.Refresh();
        }

        private DataTable ListData()
        {
            string sql;
            sql = "SELECT   row_number() over(order by 投入卷号) 序号, 投入卷号, 投入规格, 投入重量, 投入材质, 生产日期, 产出卷号, 产出规格, 产出材质, 产出重量, 产出等级, 去向, 产线名称, ";
            sql += " 班组名称, 班别,客户 FROM      View_轧硬投入产出明细表 ";
            sql += " where 1=1  and 生产日期 >='" + this.uiDatePicker1.Value.ToString() + "'";
            sql += " and 生产日期 < '" + this.uiDatePicker2.Value.AddDays(1) + "'";
            string sql2 = "select isnull(sum(投入重量),0) ,isnull(sum(产出重量),0)  FROM      View_轧硬投入产出明细表 ";
            sql2 += " where 1=1  and 生产日期 >='" + this.uiDatePicker1.Value.ToString() + "'";
            sql2 += " and 生产日期 < '" + this.uiDatePicker2.Value.AddDays(1) + "'";

            if (uiTextBox投入卷号包含.Text != "")
            {
                sql += " and 投入卷号 like '%" + this.uiTextBox投入卷号包含.Text + "%'";
                sql2 += " and 投入卷号 like '%" + this.uiTextBox投入卷号包含.Text + "%'";
            }
            if (uiTextBox产出卷号包含.Text != "")
            {
                sql += " and 产出卷号 like '%" + this.uiTextBox产出卷号包含.Text + "%'";
                sql2 += " and 产出卷号 like '%" + this.uiTextBox产出卷号包含.Text + "%'";
            }
            if (this.uiComboBox查询产线.SelectedText != "全部")
            {
                sql += " and 产线名称='" + this.uiComboBox查询产线.SelectedText + "' ";
                sql2 += " and 产线名称='" + this.uiComboBox查询产线.SelectedText + "' ";
            }
            if (this.uiComboBox查询班组.SelectedText != "全部")
            {
                sql += " and 班组名称='" + this.uiComboBox查询班组.SelectedText + "' ";
                sql2 += " and 班组名称='" + this.uiComboBox查询班组.SelectedText + "' ";
            }
            if (this.uiComboBox查询等级.SelectedText != "全部")
            {
                sql += " and 产出等级='" + this.uiComboBox查询等级.SelectedText + "' ";
                sql2 += " and 产出等级='" + this.uiComboBox查询等级.SelectedText + "' ";
            }
            sql += " order by 投入卷号 ";

            DataTable tb = pub.GetDataTable(pub.ConnectionString, sql);
            DataTable tb2 = pub.GetDataTable(pub.ConnectionString, sql2);
            if (tb2.Rows.Count > 0)
            {
                this.uiTextBox投入重量合计.Text = tb2.Rows[0][0].ToString();
                this.uiTextBox产出重量合计.Text = pub.Todecimal(tb2.Rows[0][1].ToString(), 3);
                decimal 总投入重量 = pub.Todecimal(this.uiTextBox投入重量合计.Text);
                decimal 总产出重量 = pub.Todecimal(this.uiTextBox产出重量合计.Text);

                if (总投入重量 > 0)
                {
                    this.uiTextBox产出率.Text = Math.Round((pub.Todecimal(总产出重量 / 总投入重量) * 100), 2).ToString();
                }
                else
                {
                    this.uiTextBox产出率.Text = "";
                }
            }
            this.uiTextBox卷数.Text = tb.Rows.Count.ToString();

            return tb;
        }

        private void uiDataGridView1_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e)
        {
            开始行 = e.RowIndex;
        }

        private void uiDataGridView1_CellMouseUp(object sender, DataGridViewCellMouseEventArgs e)
        {

            decimal 总投入重量 = 0;
            decimal 投入重量 = 0;
            decimal 总产出重量 = 0;

            string guid = Guid.NewGuid().ToString();
            int 总卷数 = 0;
            结束行 = e.RowIndex;
            if (开始行 >= 0 && 结束行 > 0)
            {
                if (开始行 > 结束行)
                {
                    int k = 开始行;
                    开始行 = 结束行;
                    结束行 = k;
                }
                string sql = "delete Temp_投入汇总表 where guid='" + guid + "'";
                pub.ExecuteSql(pub.ConnectionString, sql, "delete Temp_投入汇总表 ");
                for (int i = 开始行; i <= 结束行; i++)
                {
                    投入重量 = pub.Todecimal(this.uiDataGridView1.Rows[i].Cells["C投入重量"].Value.ToString());
                    string 投入卷号 = this.uiDataGridView1.Rows[i].Cells["C投入卷号"].Value.ToString();
                    sql = " insert into Temp_投入汇总表 (guid, 卷号, 投入重量) ";
                    sql += " values('" + guid + "','" + 投入卷号 + "','" + 投入重量 + "')";
                    pub.ExecuteSql(pub.ConnectionString, sql, "insert into Temp_投入汇总表 ");

                    总产出重量 += pub.Todecimal(this.uiDataGridView1.Rows[i].Cells["C产出重量"].Value.ToString());
                    总卷数++;
                }
                sql = "select  isnull(  sum(t.投入重量),0)  from  (select distinct 卷号, 投入重量 from Temp_投入汇总表 where guid='" + guid + "' ) t ";
                DataTable tb = pub.GetDataTable(pub.ConnectionString, sql);
                if (tb.Rows.Count > 0)
                {
                    总投入重量 = pub.Todecimal(tb.Rows[0][0].ToString());
                }
                else
                {
                    总投入重量 = 0;
                }
                sql = "delete Temp_投入汇总表 where guid='" + guid + "'";
                pub.ExecuteSql(pub.ConnectionString, sql, "delete Temp_投入汇总表 ");

                if (总投入重量 != 0)
                {
                    this.uiTextBox产出率.Text = Math.Round((pub.Todecimal(总产出重量 / 总投入重量) * 100), 2).ToString();
                }
                else
                {
                    this.uiTextBox产出率.Text = "";
                }
                this.uiTextBox卷数.Text = 总卷数.ToString();
                this.uiTextBox投入重量合计.Text = Math.Round(总投入重量, 3).ToString();
                this.uiTextBox产出重量合计.Text = Math.Round(总产出重量, 3).ToString();
            }
        }

        private void uiButton导出_Click(object sender, EventArgs e)
        {
            string path;
            DataTable tb = ListData();
            saveFileDialog1.Filter = "Excel文件 (*.XLSX)|*.XLSX";
            if (this.saveFileDialog1.ShowDialog(this) == DialogResult.OK)
            {
                path = this.saveFileDialog1.FileName;
                if (!File.Exists(path))
                {
                    MiniExcel.SaveAs(path, tb, true, "sheet1", ExcelType.XLSX);
                }
                else
                {
                    MessageBox.Show("文件已经存在,请重试!", "提示", MessageBoxButtons.OK);
                    return;
                }

            }
        }
    }
}
